package com.xbongbong.config;

import com.xbongbong.parent.interfaces.logger.ArchiveOperationLogHandle;
import com.xbongbong.parent.interfaces.logger.FocusOperationLogHandle;
import com.xbongbong.parent.interfaces.logger.OperationLogHandle;
import com.xbongbong.parent.interfaces.logger.PushNotifyLogHandle;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: yingjianfeng
 * @date: 2022/4/13 16:53
 * @description:
 */
@Component
public class CrmOperationLogConfig implements ApplicationContextAware {
    private Map<String, OperationLogHandle> operationLogHandleMap;
    private Map<String, PushNotifyLogHandle> pushNotifyLogHandleMap;
    private Map<String, FocusOperationLogHandle> focusOperationLogHandleMap;
    private Map<String, ArchiveOperationLogHandle> archiveOperationLogHandleMap;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        operationLogHandleMap = applicationContext.getBeansOfType(OperationLogHandle.class);
        pushNotifyLogHandleMap = applicationContext.getBeansOfType(PushNotifyLogHandle.class);
        focusOperationLogHandleMap = applicationContext.getBeansOfType(FocusOperationLogHandle.class);
        archiveOperationLogHandleMap = applicationContext.getBeansOfType(ArchiveOperationLogHandle.class);
    }

    @Bean("operationLogHandleMap")
    public Map<Integer, OperationLogHandle> initOperationLogHandle() {
        Map<Integer, OperationLogHandle> result = new HashMap<>();
        operationLogHandleMap.forEach((key, item) -> {
            item.type().forEach(k -> {
                result.put(k, item);
            });
        });
        return result;
    }

    @Bean("pushNotifyLogHandleMap")
    public Map<Integer, PushNotifyLogHandle> initPushOperationHandle() {
        Map<Integer, PushNotifyLogHandle> result = new HashMap<>();
        pushNotifyLogHandleMap.forEach((key, item) -> {
            item.type().forEach(k -> {
                result.put(k, item);
            });
        });
        return result;
    }

    @Bean("focusOperationLogHandleMap")
    public Map<Integer, FocusOperationLogHandle> initFocusOperationHandle() {
        Map<Integer, FocusOperationLogHandle> result = new HashMap<>();
        focusOperationLogHandleMap.forEach((key, item) -> {
            item.type().forEach(k -> {
                result.put(k, item);
            });
        });
        return result;
    }

    @Bean("archiveOperationLogHandleMap")
    public Map<Integer, ArchiveOperationLogHandle> initArchiveOperationHandle() {
        Map<Integer, ArchiveOperationLogHandle> result = new HashMap<>();
       archiveOperationLogHandleMap.forEach((key, item) -> {
            item.type().forEach(k -> {
                result.put(k, item);
            });
        });
        return result;
    }
}
